/**
 * Author: Max E. Kramer
 * Date: 2012
 * (c) 2013 University of Luxembourg - Interdisciplinary Centre for Security Reliability and Trust (SnT)
 * All rights reserved
 * GPLv3 to be found at: http://www.gnu.org/licenses/quick-guide-gplv3.html
 */
package lu.uni.snt.geko2.util.ui;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;

/**
 * A utility class for accessing {@link SimpleMessageConsole SimpleMessageConsoles} by name.
 * 
 * @author Max E. Kramer
 */
public final class SimpleMessageConsoleManager
{
	/** Utility classes should not have a public or default constructor. */
	private SimpleMessageConsoleManager()
	{
	}

	/** The mapping from names to simple message consoles. */
	private static final Map<String, SimpleMessageConsole> NAME_2_SIMPLE_MESSAGE_CONSOLE_MAP = new HashMap<String, SimpleMessageConsole>();

	/**
	 * Returns a console with the given name, title and shell by either reusing an existing one or creating a new one.
	 * 
	 * @param name
	 *          a name
	 * @param title
	 *          a title
	 * @param shell
	 *          a swt shell
	 * @return the console
	 */
	public static SimpleMessageConsole getConsole(final String name, final String title, final Shell shell)
	{
		// Get existing message console by name
		SimpleMessageConsole simpleMessageConsole = NAME_2_SIMPLE_MESSAGE_CONSOLE_MAP.get(name);

		/*
		 * If there is no existing SimpleMessageConsole for this name, create a new one for this console name 
		 * and add this new console into the SimpleMessageConsoleManager's console map.
		 */
		if (simpleMessageConsole == null)
		{
			simpleMessageConsole = new SimpleMessageConsole(name, title, shell);
			ConsolePlugin consolePlugin = ConsolePlugin.getDefault();
			IConsoleManager consoleManager = consolePlugin.getConsoleManager();
			consoleManager.addConsoles(new IConsole[] { simpleMessageConsole.getMessageConsole() });
			NAME_2_SIMPLE_MESSAGE_CONSOLE_MAP.put(name, simpleMessageConsole);
		}

		return simpleMessageConsole;
	}

	/**
	 * Returns a console with the given name by either reusing an existing one or creating a new one.
	 * 
	 * @param name
	 *          a name
	 * @return the console
	 */
	public static SimpleMessageConsole getConsole(final String name)
	{
		return getConsole(name, "New Default Console", null);
	}

	/**
	 * Shows the view containing the given message console.
	 * 
	 * @param simpleMessageConsole
	 *          a message console
	 */
	public static void showConsoleView(final SimpleMessageConsole simpleMessageConsole)
	{
		ConsolePlugin.getDefault().getConsoleManager().showConsoleView(simpleMessageConsole.getMessageConsole());
	}
}
